/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ekaaty.server.report;

import br.com.ekaaty.util.HibernateUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.util.Map;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;

/**
 *
 * @author otavio
 */
public abstract class GenericReport {

    protected final String path = "/src/java/br/com/ekaaty/relatorio/";
    protected final String diretorio = System.getProperty("user.dir") + path;

    protected abstract Map<String, Object> getParams();

    protected abstract String getNameFile();

    public File gerarRelatorio() throws FileNotFoundException, IOException, Exception {
        File file = new File(path);
        file = file.getAbsoluteFile();
        String arquivo = diretorio + getNameFile() + ".jasper";
        Map<String, Object> parameterMap = getParams();
        Connection conn = HibernateUtil.getInstance().connection();
        JasperPrint jp = JasperFillManager.fillReport(arquivo,
                parameterMap, conn);
        String relatoriopdf = "teste" + ".pdf";
        FileOutputStream out = new FileOutputStream(relatoriopdf);
        byte[] dados = JasperRunManager.runReportToPdf(arquivo, parameterMap, conn);
        out.write(dados);
        out.close();
        return new File(relatoriopdf);
    }
}
